#include "music.h"
#include<QString>
#include <QCoreApplication>
#include<QMediaPlayer>
#include<QUuid>
#include <QSqlQuery>
#include <QSqlError>
Music::Music()
    : isLike(false)
    , isHistory(false)
{}
Music::Music(const QUrl& url)
    : isLike(false)
    , isHistory(false)
    , musicUrl(url)
{
    //读取Url对应的歌曲文件信息，解析出源数据
    musicId = QUuid::createUuid().toString();
    parseMediaMetaData();
}
void Music::parseMediaMetaData()
{
    QMediaPlayer player;
    player.setMedia(musicUrl);

    while(!player.isMetaDataAvailable())
    {
        QCoreApplication::processEvents();
    }
    if(player.isMetaDataAvailable())
    {
        musicName = player.metaData("Title").toString();
        singerName = player.metaData("Author").toString();
        albumName = player.metaData("AlbumTitle").toString();
        duration = player.duration();

        QString fileName = musicUrl.fileName();
        int index = fileName.indexOf('-');
        if(musicName.isEmpty())
        {
            if(index != -1)
            {
                musicName = fileName.mid(0, index).trimmed();
            }
            else
            {
                musicName = fileName.mid(0, fileName.indexOf('.')).trimmed();
            }
        }

        // 作者为空
         if(singerName.isEmpty())
        {
            if(index != -1)
            {
                singerName = fileName.mid(index+1, fileName.indexOf('.')-index-1).trimmed();
            }
            else
            {
                singerName = "未知歌手";
            }
        }
         if(albumName.isEmpty())
        {
            if(index != -1)
            {
                albumName = fileName.mid(index+1, fileName.indexOf('.')-index-1).trimmed();
            }
            else
            {
                albumName = "未知专辑";
            }
        }
    }
}

void Music::insertMusicToDB()
{
    QSqlQuery query;
    query.prepare("SELECT EXISTS (SELECT 1 FROM MusicInfo WHERE musicId = ?)");
    query.addBindValue(musicId);
    if(!query.exec())
    {
        qDebug()<<"-----------SEARCH ERROR: "<<query.lastError().text();
        return;
    }
    if(query.next())
    {
        bool isExists = query.value(0).toBool();
        if(isExists)
        {
            query.prepare("UPDATE MusicInfo SET isLike = ?, isHistory = ? WHERE musicId = ?");
            query.addBindValue(isLike? 1 : 0);
            query.addBindValue(isHistory? 1 : 0);
            query.addBindValue(musicId);
            if(!query.exec())
            {
                qDebug()<<"-------------UPDATA ERROR"<<query.lastError().text();
            }
            qDebug()<<"UPDATA MUSIC INFORMATION: "<<musicName<<" "<<musicId;
        }
        else
        {
            query.prepare("INSERT INTO MusicInfo(musicId, musicName, musicSinger, albumName, musicUrl,\
                                duration, isLike, isHistory)\
                                VALUES(?,?,?,?,?,?,?,?)");
            query.addBindValue(musicId);
            query.addBindValue(musicName);
            query.addBindValue(singerName);
            query.addBindValue(albumName);
            query.addBindValue(musicUrl.toLocalFile());
            query.addBindValue(duration);
            query.addBindValue(isLike ? 1 : 0);
            query.addBindValue(isHistory? 1 : 0);
            if(!query.exec())
            {
                qDebug()<<"insert error: "<<query.lastError().text();
                return;
            }
            qDebug()<<"INSERT MUSIC INFORMATION:"<<query.lastError().text();
        }
    }
}
void Music::setIsLike(bool isLike)
{
    this->isLike = isLike;
}

void Music::setIsHistory(bool isHistory)
{
    this->isHistory = isHistory;
}

void Music::setMusicName(const QString &musicName)
{
    this->musicName = musicName;
}

void Music::setSingerName(const QString &singerName)
{
    this->singerName = singerName;
}

void Music::setAlbumName(const QString &albumName)
{
    this->albumName = albumName;
}

void Music::setDuration(const qint64 duration)
{
    this->duration = duration;
}

void Music::setMusicUrl(const QUrl &url)
{
     this->musicUrl = url;
}

void Music::setMusicId(const QString &musicId)
{
    this->musicId = musicId;
}

bool Music::getIsLike()
{
    return isLike;
}

bool Music::getIsHistory()
{
     return isHistory;
}

QString Music::getMusicName()
{
    return musicName;
}

QString Music::getSingexrName()
{
    return singerName;
}

QString Music::getAlbumName()
{
    return albumName;
}

qint64 Music::getDuraxtion()
{
    return duration;
}

QUrl Music::getMusicUrl()
{
     return musicUrl;
}

QString Music::getMusicId()
{
    return musicId;
}

QString Music::getLrcFilePath() const
{
    QString path = musicUrl.toLocalFile();
    qDebug() << "[Debug] Original music path:" << path;
     path.replace(".mp3", ".lrc");
     path.replace(".flac", ".lrc");
     path.replace(".mpga", ".lrc");
     qDebug()<<"path:"<<path;
     qDebug() << "[Debug] Generated LRC path:" << path;  // 打印生成的歌词路径

     return path;
}






